package com.synnex.mobile.servier.web.msgBoard.controller.mobile;

import com.synnex.mobile.generic.core.json.JSONModel;
import com.synnex.mobile.generic.log.dao.annotation.LogClass;
import com.synnex.mobile.generic.log.dao.annotation.LogMethod;
import com.synnex.mobile.generic.web.anotation.mapping.RequestMapping;
import com.synnex.mobile.generic.web.view.ModelAndView;
import com.synnex.mobile.servier.core.msgBoard.bo.MsgBo;
import com.synnex.mobile.servier.core.updateTime.bo.UpdateTime;
import com.synnex.mobile.servier.core.updateTime.bo.UpdateTimeCriterion;
import com.synnex.mobile.servier.web.msgBoard.controller.AbstractMsgBoardController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author lancec
 */
@RequestMapping(url = "/mobile/msgBoard")
@LogClass(name = "MsgBo")
public class MobileMsgBoardController extends AbstractMsgBoardController {
    private static final String[] EXCLUDE_PROPERTY = new String[]{"updateDate", "id",};

    @RequestMapping(url = "/listMsg.do")
    @LogMethod(name = "list msg board")
    public ModelAndView listMsg(HttpServletRequest request, HttpServletResponse response) {
        try {
            ModelAndView modelAndView = this.getMsgList(request, response);
            return modelAndView;
        } catch (Exception e) {
            return createErrorJSONModelAndView(e);
        }
    }

    @RequestMapping(url="/addMsg.do")
    @LogMethod(name="add message to message board")
    public ModelAndView addMsg(HttpServletRequest request,HttpServletResponse response){

        String user = request.getParameter("user");
        String content = request.getParameter("content");
        if(null==user || "".equals(user)){
            user="游客";
        }
        try{
//            content = new String(content.getBytes("ISO-8859-1"),"utf-8");
//            user = new String(user.getBytes("ISO-8859-1"),"utf-8");
            MsgBo msg = new MsgBo();
            msg.setContent(content);
            msg.setUser(user);
            msgBoardService.saveMsg2MsgBoard(msg);
            return createJSONModelAndView(JSONModel.create());
        }catch (Exception e){
            return createErrorJSONModelAndView(e);
        }
    }


    public ModelAndView getMsgList(HttpServletRequest request, HttpServletResponse response) {
        String updateTimeStr = request.getParameter("updateTime");

        JSONModel jsonModel = JSONModel.create().addExcludes(EXCLUDE_PROPERTY);

        Long returnTime = null;
        UpdateTimeCriterion criterion = new UpdateTimeCriterion();
        criterion.setUrl(getUrl(request));

        Long updateTime = null;
        if (null != updateTimeStr && !"".equals(updateTimeStr)) {
            updateTime = Long.parseLong(updateTimeStr);
            criterion.setUpdateTime(updateTime);
        }
        UpdateTime updateTimeFromDB = updateTimeService.getUpdateTime(criterion);

        UpdateTime cache = new UpdateTime();

        if (null == updateTimeFromDB) {
            Long newTime = System.currentTimeMillis();
            cache.setUpdateTime(newTime);
            cache.setUrl(getUrl(request));
            updateTimeService.saveUpdateTime(cache);

            Map<String, Object> results = super.MsgList(request);
            jsonModel.setData(results);
            jsonModel.setStatus(JSONModel.STATUS.SUCCESS);
            returnTime = newTime;
        } else {
            if (updateTime == null || updateTimeFromDB.getUpdateTime().longValue() != updateTime.longValue()) {
                returnTime = updateTimeFromDB.getUpdateTime();
                jsonModel.setStatus(JSONModel.STATUS.SUCCESS);
                Map<String, Object> results = super.MsgList(request);
                jsonModel.setData(results);
            } else {
                jsonModel.setStatus(JSONModel.STATUS.CACHEED);
                returnTime = updateTimeFromDB.getUpdateTime();
            }
        }
        jsonModel.addMoreProperty("updateTime", returnTime.toString());

        return createJSONModelAndView(jsonModel);

    }

    protected String getUrl(HttpServletRequest request) {
        String path = request.getServletPath();
        String page = request.getParameter("pageNo");
        return path + "?pageNo=" + page;
    }
}
